package com.ajnp.springcloud.controller;

import com.ajnp.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
@RequestMapping("/payment")
public class PaymentController {

    static int js = 1;

    @Resource
    private PaymentService paymentService;

    //正常执行
    @GetMapping("/hystrix/ok/{id}")
    public String is_OK(@PathVariable("id") Integer id) {

        String isok = paymentService.PaymentInfo_OK(id);
        log.info("******isok " + isok + "次数：" + js);
        js++;
        return isok;
    }

    //使用工具jmeter进行高并发次数访问20000次 时 所有cpu调度线程执行out方法 由于在一个controller内
    //tomcat的默认的工作线被打满，没有多余的线程来分解压力和处理
    //导致请求isok方法也发生卡顿
    @GetMapping("/hystrix/out/{id}")
    public String no_OK(@PathVariable("id") Integer id) {
        //延迟5秒
        String nook = paymentService.PaymentInfo_Timeout(id);
        log.info("******nook " + nook);
        return nook;
    }

    //服务熔断

    @GetMapping("/hystrix/rd/{id}")
    public String paymentCircuitBreaker(@PathVariable("id") Integer id) {
        String rd = paymentService.paymentCircuitBreaker(id);
        log.info(rd);
        return rd;
    }


}
